package com.controller;

import com.entity.PasswordEntity;
import com.entity.ResultEntity;
import com.myenum.ResponseEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriComponentsBuilder;

/**
 * Created by Germmy on 2018/3/31.
 */
@RestController
@RequestMapping("/pmc")
public class PasswordMgrController {


    //添加一个日志器
    private static final Logger logger = LoggerFactory.getLogger(PasswordMgrController.class);


    /**
     * @api {get} http://xxx/test.maven.webapp/pmc/pwdquery/{id}  根据id查询密码实体
     * @apiName GetPasswordEntity
     * @apiGroup PasswordEntity
     * @apiParam {Long} id 密码实体的唯一编号
     * @apiSuccess {Long} id 密码实体的唯一编号
     * @apiSuccess {String} webSiteName  网站名称
     * @apiSuccess {String} userName  用户名
     * @apiSuccess {String} password  密码
     * @apiSuccessExample Success-Response:
     * 0000000 OK
     * {
     * "errCode": "0000000",
     * "errorMessage": "success",
     * "data": {
     * "webSiteName": "jd",
     * "userName": "wandejun1012",
     * "password": "123456",
     * "id": 1
     * }
     * }
     * @apiError 1000001 该密码实体找不到
     * @apiError 1000002 xxxx
     * @apiError 9999999 服务器开小差了
     * @apiErrorExample Error-Response:
     * {
     * "errCode": "1000001",
     * "errorMessage": "该密码实体找不到",
     * "data": null
     * }
     */
    @RequestMapping(value = "/pwdquery/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<ResultEntity<PasswordEntity>> getUser(@PathVariable("id") long id) {
        //输出日志文件
        logger.info("password query");

        ResultEntity<PasswordEntity> resultEntity = new ResultEntity<>();

        PasswordEntity pe = null;
        if (id == 1) {
            pe = new PasswordEntity();
            pe.setID(1L);
            pe.setWebSiteName("jd");//TODO 这地方用中文会乱码，后续解决
            pe.setUserName("wandejun1012");
            pe.setPassword("123456");

            //设置re
            resultEntity.setErrCode(ResponseEnum.SUCCESS.getErrorCode());
            resultEntity.setErrorMessage(ResponseEnum.SUCCESS.getErrorMessage());

//            resultEntity.setResponseEnum(ResponseEnum.SUCCESS);//必须用上面的，否则出来的api就是以responseenum为key,别人根本看不懂这是啥玩意，没有errorcode,errormessage清晰

            resultEntity.setData(pe);

        } else {
            resultEntity.setErrCode(ResponseEnum.PASSWORD_NOT_EXIST.getErrorCode());
            resultEntity.setErrorMessage(ResponseEnum.PASSWORD_NOT_EXIST.getErrorMessage());
//            resultEntity.setResponseEnum(ResponseEnum.PASSWORD_NOT_EXIST);
            return new ResponseEntity(resultEntity, HttpStatus.NOT_FOUND);//这个返回值不太对，前端捕捉不了
        }

        //返回
//        return new ResponseEntity<ResultEntity<PasswordEntity>>(re, HttpStatus.OK);
        return new ResponseEntity(resultEntity, HttpStatus.OK);
    }


    //注意：在调用时必须加上后面的/,否则映射不到
    /**
     * @api {post} http://xxx/test.maven.webapp/pmc/pwdCreate  创建密码实体
     * @apiName pwdCreate
     * @apiGroup pwdCreate
     * @apiParam {String} webSiteName  网站名称
     * @apiParam {String} userName  用户名
     * @apiParam  {String} password  密码
     * @apiSuccess {Long} id 密码实体的唯一编号
     * @apiSuccess {String} webSiteName  网站名称
     * @apiSuccess {String} userName  用户名
     * @apiSuccess {String} password  密码
     * @apiSuccessExample Success-Response:
     * 0000000 OK
     * {
     * "errCode": "0000000",
     * "errorMessage": "success",
     * "data": {
     * "webSiteName": "jd",
     * "userName": "wandejun1012",
     * "password": "123456",
     * "id": 1
     * }
     * }
     * @apiError 1000011 重复创建
     * @apiError 1000002 xxxx
     * @apiError 9999999 服务器开小差了
     * @apiErrorExample Error-Response:
     * {
     * "errCode": "1000011",
     * "errorMessage": "重复创建",
     * "data": null
     * }
     */
    @RequestMapping(value = "/pwdCreate", method = RequestMethod.POST)
    public ResponseEntity<PasswordEntity> createPwd(@RequestBody PasswordEntity passwordEntity, UriComponentsBuilder ucBuilder) {
        System.out.println("Creating PasswordEntity " + passwordEntity.getPassword());
        HttpHeaders headers = new HttpHeaders();
        return new ResponseEntity<PasswordEntity>(passwordEntity, HttpStatus.OK);
    }


}
